package com.ebt.m.policy.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;

import com.ebt.m.R;
import com.ebt.m.adapter.SimpleAdapter;
import com.ebt.m.adapter.SimpleHolder;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.data.rxModel.apibean.FilterDataBeanItem;
import com.ebt.m.widget.GridSpaceItemDecoration;

import java.util.HashMap;
import java.util.List;


/**
 * @author Damon.Han
 */
public class FilterContentView extends IView {

    private RecyclerView recyclerView;
    List<FilterDataBeanItem> list;

    public FilterContentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View view = inflate(context, R.layout.view_online_policy_filter_content, this);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.addItemDecoration(new GridSpaceItemDecoration(4, 20, false));
    }

    public FilterContentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterContentView(Context context) {
        this(context, null);
    }


    @Override
    public void update(Object... args) {
        list = (List<FilterDataBeanItem>) args[0];
        SimpleAdapter adapter = new SimpleAdapter<FilterDataBeanItem>(getContext(), R.layout.view_online_policy_filter_tag, list) {
            @Override
            public void onBindView(SimpleHolder holder, int position) {
                FilterDataBeanItem item = list.get(position);
                CheckBox checkBox = (CheckBox) holder.getView(R.id.text);
                checkBox.setText(item.getLabel());
            }
        };
        adapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                selectedItem(view, (FilterDataBeanItem) list.get(position));
            }
        });
        recyclerView.setAdapter(adapter);
    }


    /**
     * 选中一个品牌
     *
     * @param view
     */
    private void selectedItem(View view, FilterDataBeanItem itemBean) {
        for (int i = 0; i < recyclerView.getChildCount(); i++) {
            ((CheckBox) recyclerView.getChildAt(i)).setChecked(false);
        }
        ((CheckBox) view).setChecked(true);
    }

    /**
     *
     */
    public FilterDataBeanItem getChecked() {
        HashMap<String, String> map = new HashMap<>();
        CheckBox box;
        for (int i = 0; i < recyclerView.getChildCount(); i++) {
            box = (CheckBox) recyclerView.getChildAt(i);
            if (box.isChecked()) {
                return list.get(i);
            }
        }
        return null;
    }

    public void reset() {
        for (int i = 0; i < recyclerView.getChildCount(); i++) {
            ((CheckBox) recyclerView.getChildAt(i)).setChecked(false);
        }
    }
}
